/**
 *
 *  Copyright (c) 2011, AstroDAbis
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *      * Redistributions of source code must retain the above copyright notice,
 *        this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright notice,
 *        this list of conditions and the following disclaimer in the documentation
 *        and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package org.jiscinvolve.astrodabis.mallow.impl.model.jpa;

import java.util.Date;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;

import javax.persistence.PreUpdate;
import javax.persistence.PrePersist;

import javax.persistence.Version;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import javax.persistence.OneToMany;
import javax.persistence.ManyToOne;
import javax.persistence.JoinColumn;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

//
// Spring injection support.
//import org.springframework.beans.factory.annotation.Autowire;
//import org.springframework.beans.factory.annotation.Required;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Configurable;

//import org.springframework.context.annotation.Scope;
//import org.springframework.context.annotation.ScopedProxyMode;

import org.jiscinvolve.astrodabis.mallow.model.api.Account;
import org.jiscinvolve.astrodabis.mallow.model.jpa.ManagedObject;
import org.jiscinvolve.astrodabis.mallow.model.jpa.PersistentObject;
import org.jiscinvolve.astrodabis.mallow.model.jpa.PersistentManager;

import org.jiscinvolve.astrodabis.mallow.impl.model.api.AccountImpl;

/**
 * Base class implementation for PersistentObjects with owner and created and modified dates.
 *
 */
@MappedSuperclass
abstract public class ManagedObjectImpl
extends PersistentObjectImpl
implements ManagedObject
    {
    /**
     * Our debug logger.
     * 
     */
    private static Log log = LogFactory.getLog(ManagedObjectImpl.class);

    /**
     * Protected constructor.
     *
     */
    protected ManagedObjectImpl()
        {
        super();
        log.debug("ManagedObjectImpl()");
        }

    /**
     * Protected constructor.
     *
     */
    protected ManagedObjectImpl(AccountImpl owner)
        {
        super();
        log.debug("ManagedObjectImpl(AccountImpl)");
        init(owner);
        }

    /**
     * Initialise our create date and owner.
     *
     */
    protected void init(AccountImpl owner)
        {
        log.debug("ManagedObjectImpl.init(AccountImpl)");
        this.owner   = owner ;
        this.created = new Date();
        }

    /**
     * The date created.
     *
     */
    @Column(name="created")
    @Temporal(TemporalType.TIMESTAMP)
    private Date created ;

    @Override
    public Date created()
        {
        return this.created ;
        }

    /**
     * The date modified.
     *
     */
    @Version
    @Column(name="modified")
    @Temporal(TemporalType.TIMESTAMP)
    private Date modified ;

    @Override
    public Date modified()
        {
        return this.modified ;
        }

    /**
     * The object owner.
     *
     */
    @ManyToOne
    @JoinColumn(name="owner", nullable=false)
    private AccountImpl owner;

    @Override
    public AccountImpl owner()
        {
        return this.owner ;
        }

    /**
     * JPA create hook.
     * Not always triggered, ignored in JUnit tests.
     * Easier to do this in constructor.
     * http://stackoverflow.com/questions/4133287/preupdate-and-prepersist-in-hibernate-jpa-using-session
     * http://notatube.blogspot.com/2010/03/hibernate-using-event-listener-to-set.html
     *
    @PrePersist
    protected void onCreate() 
        {
        log.debug("PersistentObjectImpl.onCreate()");
        this.created = new Date();
        }
     */

    /**
     * JPA update hook.
     * Not always triggered, ignored in JUnit tests.
     * Easier to user @Version annotation.
     * http://stackoverflow.com/questions/4133287/preupdate-and-prepersist-in-hibernate-jpa-using-session
     * http://notatube.blogspot.com/2010/03/hibernate-using-event-listener-to-set.html
     *
    @PreUpdate
    protected void onUpdate()
        {
        log.debug("PersistentObjectImpl.onUpdate()");
        this.modified = new Date();
        }
     */


    }

